package com.lzj.demo.design.controller;

import com.lzj.demo.design.common.ServerResult;
import com.lzj.demo.design.chain.ChainPatternDemo;
import com.lzj.demo.design.chain.fzw.chain.AuthChainDemo;
import com.lzj.demo.design.vo.OrderQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;


@RestController
@RequestMapping("/demo/design")
@Slf4j
public class ChainController {
    @Resource
    public ChainPatternDemo chainPatternDemo;
    @Resource
    public AuthChainDemo authChainDemo;

    /**
     * 下订单时按顺序校验参数
     * */
    @PostMapping("/chain")
    public ServerResult chain(@RequestBody OrderQueryVO request){
        ServerResult response = new ServerResult();
        log.info("response:{}",chainPatternDemo.exec(request,response));
        return response;
    }

    /**
     * 审批流程
     * */
    @PostMapping("/chain2")
    public ServerResult chain2(){
        authChainDemo.exec();
        return ServerResult.success();
    }

    /**
     * 全局异常处理测试
     * */
    @PostMapping("/test")
    public ServerResult test(@Validated @RequestBody Map map){
        Integer i = (Integer)map.get("param");
        int result = i/0;
        return ServerResult.success();
    }
}
